package com.hengpeng.itfinbase.persist;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.hengpeng.common.acl.vo.Passport.Token;
import com.hengpeng.common.persist.entity.AuditedEntity;
import com.hengpeng.common.web.annotation.HtmlInput;
import com.hengpeng.common.web.annotation.HtmlInputPattern;
import com.hengpeng.common.web.annotation.HtmlInputType;
import com.hengpeng.itfinbase.idAssign.AbstractAssign18IdEntity;

/**
 * <pre>
 * 系统用户信息
 * </pre>
 * @author linriqing
 * @version 1.0, 2010-7-19
 */
@Entity
public class AppUser extends AbstractAssign18IdEntity implements AuditedEntity
{
	public static enum UserStatus
	{
		CLOSE("关闭"), NORMAL("正常"), STOP("暂停使用");

		private String text;

		UserStatus(String text)
		{
			this.text = text;
		}

		public String getText()
		{
			return text;
		}

		public void setText(String text)
		{
			this.text = text;
		}
	}

	private static final long serialVersionUID = -6082641296878214602L;

	@HtmlInput(label = "创建时间", type = HtmlInputType.TEXT, index = 10, nullable = true)
	private Date createTime;

	@Column(length = 30)
	@HtmlInput(label = "邮箱", pattern = HtmlInputPattern.EMAIL, maxLength = 30, type = HtmlInputType.TEXT, index = 7, nullable = false)
	private String email;

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "AppUserGroup", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "groups_id") })
	@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
	private Set<AppGroup> groups;

	@HtmlInput(label = "最近登陆时间", type = HtmlInputType.TEXT, index = 11, nullable = true)
	private Date lastLogin;

	@HtmlInput(label = "最近更新时间", type = HtmlInputType.TEXT, index = 12, nullable = true)
	private Date lastUpdate;

	@Column(length = 30, unique = true)
	@HtmlInput(label = "登录名", type = HtmlInputType.TEXT, maxLength = 30, index = 2, nullable = false)
	private String loginName;

	@OneToMany(cascade = { CascadeType.REMOVE }, fetch = FetchType.EAGER, mappedBy = "userId")
	private Set<ShortcutMenu> menuSet;

	@Column(length = 20)
	@HtmlInput(label = "昵称", type = HtmlInputType.TEXT, maxLength = 20, index = 3, nullable = false)
	private String nickName;

	@Column(length = 40)
	@HtmlInput(label = "密码", type = HtmlInputType.PASSWORD, maxLength = 20, index = 4, nullable = false)
	private String password;

	@Column(length = 20)
	@HtmlInput(label = "联系电话", type = HtmlInputType.TEXT, maxLength = 20, index = 5, nullable = false)
	private String phone;

	@Column(length = 20)
	@HtmlInput(label = "真实姓名", type = HtmlInputType.TEXT, maxLength = 20, index = 6, nullable = false)
	private String realName;

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "AppUserRole", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "roles_id") })
	@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
	private Set<AppRole> roles;

	@Column(length = 10)
	@Enumerated(javax.persistence.EnumType.STRING)
	@HtmlInput(label = "状态", type = HtmlInputType.SELECT, labelProperty = "text", index = 8, nullable = false)
	private UserStatus state;


	@Column(length = 10)
	@Enumerated(javax.persistence.EnumType.STRING)
	@HtmlInput(label = "系统用户属性", type = HtmlInputType.SELECT, labelProperty = "text", index = 9, nullable = false)
	private Token token;

	@Column(length = 20, unique = true)
	@HtmlInput(label = "用户编号", type = HtmlInputType.TEXT, maxLength = 20, index = 1, nullable = false, tips = "类似于客服号")
	private String userNo;

	public Date getCreateTime()
	{
		return this.createTime;
	}

	public String getEmail()
	{
		return this.email;
	}

	public Set<AppGroup> getGroups()
	{
		return groups;
	}

	public Date getLastLogin()
	{
		return this.lastLogin;
	}

	public Date getLastUpdate()
	{
		return this.lastUpdate;
	}

	public String getLoginName()
	{
		return this.loginName;
	}

	public Set<ShortcutMenu> getMenuSet()
	{
		return menuSet;
	}

	public String getNickName()
	{
		return this.nickName;
	}

	public String getPassword()
	{
		return this.password;
	}

	public String getPhone()
	{
		return this.phone;
	}

	public String getRealName()
	{
		return this.realName;
	}

	public Set<AppRole> getRoles()
	{
		return roles;
	}

	public UserStatus getState()
	{
		return this.state;
	}

	public Token getToken()
	{
		return token;
	}

	public String getUserNo()
	{
		return this.userNo;
	}

	public void setCreateTime(Date createTime)
	{
		this.createTime = createTime;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public void setGroups(Set<AppGroup> groups)
	{
		this.groups = groups;
	}

	public void setLastLogin(Date lastLogin)
	{
		this.lastLogin = lastLogin;
	}

	public void setLastUpdate(Date lastUpdate)
	{
		this.lastUpdate = lastUpdate;
	}

	public void setLoginName(String loginName)
	{
		this.loginName = loginName;
	}

	public void setMenuSet(Set<ShortcutMenu> menuSet)
	{
		this.menuSet = menuSet;
	}

	public void setNickName(String nickName)
	{
		this.nickName = nickName;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public void setPhone(String phone)
	{
		this.phone = phone;
	}

	public void setRealName(String realName)
	{
		this.realName = realName;
	}

	public void setRoles(Set<AppRole> roles)
	{
		this.roles = roles;
	}

	public void setState(UserStatus state)
	{
		this.state = state;
	}

	public void setToken(Token token)
	{
		this.token = token;
	}

	public void setUserNo(String userNo)
	{
		this.userNo = userNo;
	}

}